<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="activityList">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="头像" align="center" width="150">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="报名信息" align="left" prop="phone" width="250">
        <template slot-scope="scope">
          <div style="font-size: 12px; line-height: 1.5;">
          真实姓名: {{ scope.row.forms.realname || '-' }} <br>
          手机号: {{ scope.row.forms.phone || '-' }} <br>
          公司: {{ scope.row.forms.company_name || '-' }} <br>
          职务: {{ scope.row.forms.position || '-' }} <br>
          邮箱: {{ scope.row.forms.email || '-' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_join_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
         <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">状态审核</el-button>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createdAt"></el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body v-if="open">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="申请信息" prop="title">
          <div style="line-height: 1.5; margin-top: 8px;">
          真实姓名: {{ form.forms.realname || '-' }} <br>
          手机号: {{ form.forms.phone || '-' }} <br>
          公司: {{ form.forms.company_name || '-' }} <br>
          职务: {{ form.forms.position || '-' }} <br>
          邮箱: {{ form.forms.email || '-' }}
          </div>
        </el-form-item>

        <el-form-item label="当前状态" prop="status">
          {{ getStatusText(form.status) }}
        </el-form-item>
        <el-form-item label="状态" prop="status" v-if="form.status == 0">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="3">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="签到" prop="status" v-if="form.status == 1">
          <el-radio-group v-model="form.status">
            <el-button @click="handleSign" type="primary">确认签到</el-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { applyListActivity, getActivity, delActivity, addActivity, activeApplyStatus } from "@/api/mobile/activity"

export default {
  props: {
    activeId: Number
  },
  dicts: ['activity_join_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getStatusText(code){
      const dart = this.dict.type.activity_join_status.find(item => Number(item.value) === code);
      if(dart) {
        return dart.label
      }else {
        return '未知'
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      applyListActivity({...this.queryParams, activeId: this.activeId}).then(response => {
        this.activityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
      }
      this.resetForm("form")
      this.area = []
      this.dateRange = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "申请审核"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = "申请审核"
    },
    handleSign(){
      this.form.status = 2;
      this.submitForm()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            activeApplyStatus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除信息编号为"' + ids + '"的数据项？').then(function() {
        return delActivity(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    },
  }
}
</script>
